/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyTag;

import MyBean.MyConnection;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author amaster
 */
public class ListBookTag extends SimpleTagSupport {

    private String catid;

    public void setCatid(String catid) {
        this.catid = catid;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();
        if (catid != null) {
            try {
                String table = "<table border='1' width='500'>";
                table += "<tr><td><b>Ten DT</td>"
                        + "<td><b>Man hinh</td>"
                        + "<td><b>Gia</td>"
                        + "<td><b>Action</td>"
                        + "</tr>";

                Connection con = MyConnection.getConnection();
                Statement selectSt = null;
                try {
                    selectSt = con.createStatement();
                } catch (SQLException ex) {
                    Logger.getLogger(ListBookTag.class.getName()).log(Level.SEVERE, null, ex);
                }
                ResultSet rs = null;
                try {
                    rs = selectSt.executeQuery("Select * from [mobile].[dbo].[dienthoai] where hangID =" + catid);
                } catch (SQLException ex) {
                    Logger.getLogger(ListBookTag.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    while (rs.next()) {
                        table += "<tr>";
                        table += "<td>" + rs.getString("dtTen") + "</td>";
                        table += "<td>" + rs.getFloat("dtMH") + "</td>";
                        table += "<td>" + rs.getFloat("dtGia") + "</td>";
                        table += "<td><a href='Edit?cat="+rs.getInt("dtID")+"'>edit</td>";
                        table += "</tr>";
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(ListBookTag.class.getName()).log(Level.SEVERE, null, ex);
                }

                table += "</table>";
                out.println(table);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ListBookTag.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(ListBookTag.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}
